<template>
  <div
    class='container'
  >
    <el-card class="box-card">
      <el-button
        class="button"
        icon="el-icon-edit"
        type="success"
        size="small"
        @click="addSubjects"
      >
        新增学科
      </el-button>
      <el-form :inline="true" label-width="90px">
        <el-form-item label="学科名称">
            <el-input placeholder="请输入" />
          </el-form-item>
        <el-form-item size="large">
          <el-button size="small" >清除</el-button>
          <el-button size="small" type="primary">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-alert
        :title="'数据一共'+ subjectsList.counts + '条'"
        type="info"
        show-icon
        class="record"
      >
      </el-alert>
      <my-table
        :table-props="{
          data: subjectsList.items,
          border: true
        }"
        :columns="[
          { label: '序号', prop: 'index', width: 100, align: 'center' },
          { label: '学科名称', prop: 'subjectName', width: 150, align: 'center' },
          { label: '创造者', prop: 'username', width: 150, align: 'center' },
          { label: '创造日期', prop: 'addDate', width: 150, align: 'center' },
          { label: '前台是否显示', prop: 'isFrontDisplay', width: 100, align: 'center' },
          { label: '二级目录', prop: 'twoLevelDirectory', width: 100, align: 'center' },
          { label: '标签', prop: 'tags', width: 100, align: 'center' },
          { label: '题目数量', prop: 'totals', width: 100, align: 'center' },
          { label: '操作', slotName: 'operators', width: 300, align: 'center' }, // 带插槽
        ]"
        :pagination-props="{
          layout: 'total, pager',
          total: subjectsList.counts
        }"
        :pagination-events="events"
      >
        <template v-slot:operators="scope">
          <el-button @click="classify(scope.row)" type="text">学科分类</el-button>
          <el-button @click="sbjectLabels(scope.row)" type="text">学科标签</el-button>
          <el-button type="text" >修改</el-button>
          <el-button @click="deleteSubjects(scope.row)" type="text" >删除</el-button>
        </template>
      </my-table>
    </el-card>
        <el-dialog
      title="新增学科"
      :visible.sync="addUserVisible"
      width="30%"
      @close="onAddEmployeeClose"
    >
      <el-form
        :model="addSubjectsGoTo"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="120px"
        class="demo-ruleForm"
        style="width: 100%; margin-left: 10px;"
      >
        <el-form-item
          label="学科名称"
          prop="subjectName"
        >
          <el-input
            type="text"
            v-model="addSubjectsGoTo.subjectName"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="是否显示"
          prop="isFrontDisplay"
        >
          <el-switch
            v-model="addSubjectsGoTo.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onAddEmployeeClose">取 消</el-button>
        <el-button type="primary" @click="onAddEmployeeOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import * as subjectsApi from '@/api/hmmm/subjects'
export default {
  data() {
    const validateNull = (rule, value, callback) => {
      if (!value.trim()) {
        callback(new Error(this.$t('值不能为空')))
      } else {
        callback()
      }
    }
    return {
      currentPage: 1,
      pageSize: 10,
      events: {
        'current-change': this.onCurrentPageChange
      },
      addSubjectsGoTo: {
        subjectName: '',
        isFrontDisplay: '',
      },

      addUserVisible: false,
      addFormRules: {
        subjectName:[{ required: true, trigger: 'blur', validator: validateNull }],
      }
    }
  },
  computed: {
    ...mapState('subjects', [
      'subjectsList'
    ])
  },
  methods: {
    ...mapActions('subjects', [
      'updateSubjectsList'
    ]),
    onCurrentPageChange(currentPage) {
      this.currentPage = currentPage
      this.updateSubjectsList({
        page: this.currentPage,
        size: this.pageSize,
      });
    },
    // 删除学科
    deleteSubjects(row) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          subjectsApi.remove({
            id: row.id
          })
          .then(() => {
            this.$message({
              type: 'success',
              message: '删除成功'
            });
          })
          this.updateSubjectsList({
            page: this.currentPage,
            size: this.pageSize,
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    addSubjects() {
      this.addUserVisible = true
    },
    onAddEmployeeClose() {
      this.addUserVisible = false
    },
    async onAddEmployeeOk() {
      await subjectsApi.add(this.addSubjectsGoTo)
        .then(msg => this.$message.success(msg))
        .catch(msg => this.$message.error(msg))
      // 请求完成后刷新列表
      this.updateSubjectsList({
        page: this.currentPage,
        size: this.pageSize
      })
      this.addUserVisible = false
    },
    classify() {
      this.$router.push('/subjects/directorys');
    },
    sbjectLabels() {
      this.$router.push('/subjects/tags');
    }

  },
   async created() {
    await this.updateSubjectsList({
      page: this.currentPage,
      size: this.pageSize,
    });
  },
}
</script>

<style scoped lang='scss'>
.button {
  float: right;
}
.record {
  margin-bottom: 12px;
}
</style>
